function getDoctype() {
    var doc = document.doctype;
    if (!doc) {
        return '';
    }
    var documentType = '<!DOCTYPE ' + doc.name;
    if (doc.publicId != '') {
        documentType +=  ' PUBLIC "' + doc.publicId + '"';
    }
    if (doc.systemId != '') {
        documentType += ' "' + doc.systemId + '"';
    }
    documentType += ">\n";
    return documentType;
}

function getSource() {

    var theDiv = document.getElementById("repeat_mode_button");
    var currPosition = getStyle(theDiv, "background-position");
    if(currPosition.indexOf("-601px -186px")!=-1){
      return "Off";
    }else if(currPosition.indexOf("-673px -405px")!=-1){
      return "Off"
    }else if(currPosition.indexOf("-615px -20px")!=-1){
      return "On";
    }else if(currPosition.indexOf("-505px -206px")!=-1){
      return "On"
    }else if(currPosition.indexOf("-735px -256px")!=-1){
      return "Single";
    }else if(currPosition.indexOf("-381px -256px")!=-1){
      return "Single"
    }else{
      return null;
    }
}

function sendSourceCode() {
    chrome.extension.sendRequest({source: getSource()});
}

function getStyle(el, style) {
   if(!document.getElementById) return;
   
     var value = el.style[toCamelCase(style)];
   
    if(!value)
        if(document.defaultView)
            value = document.defaultView.
                 getComputedStyle(el, "").getPropertyValue(style);
       
        else if(el.currentStyle)
            value = el.currentStyle[toCamelCase(style)];
     
     return value;
}

function toCamelCase( sInput ) {
    var oStringList = sInput.split('-');
    if(oStringList.length == 1)    
        return oStringList[0];
    var ret = sInput.indexOf("-") == 0 ? 
    	oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
    for(var i = 1, len = oStringList.length; i < len; i++){
        var s = oStringList[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1)
    }
    return ret;
}

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    sendSourceCode();
});

document.getElementById("repeat_mode_button").addEventListener("DOMSubtreeModified", function() {
    sendSourceCode();
  });
